package com.hmall.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.user.mapper.UserMapper;
import com.hmall.user.pojo.Address;
import com.hmall.user.pojo.User;
import com.hmall.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("userservice")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = false)
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    //注入mapper
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Address> findAddressByUid(Long uid) {
        return userMapper.findAddress(uid);
    }

    /**
     * 根据地址id查询地址
     *
     * @param id 地址id
     * @return
     */
    @Override
    public Address findAddresses(Long id) {
        return userMapper.findAddresses(id);
    }

    /**
     * 根据用户id查询用户信息
     *
     * @param userId 用户id
     * @return 返回user对象
     */
    @Override
    public User findUserById(Long userId) {
        return userMapper.findUserId(userId);
    }

    @Override
    public void updateUser(Long money, Long id) {
        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.eq("id",id).set("balance",money);
        //修改
        this.update(userUpdateWrapper);
    }


}
